import { fileURLToPath, URL } from "node:url";
// 自动导入vue中hook reactive ref等
import AutoImport from "unplugin-auto-import/vite"
//自动导入ui-组件 比如说ant-design-vue  element-plus等
import Components from 'unplugin-vue-components/vite';
import { compression } from 'vite-plugin-compression2'
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import babel from '@rollup/plugin-babel'
import seoPrerender from 'vite-plugin-seo-prerender'
import UnoCSS from 'unocss/vite'
import { ElementPlusResolver, AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
const PrerenderSPAPlugin = require("prerender-spa-plugin");
// import obfuscatorPlugin from 'rollup-plugin-javascript-obfuscator'
// import { compression } from 'vite-plugin-compression2'
// import { viteObfuscateFile } from 'vite-plugin-obfuscator'
const dependencies = require('./package.json').dependencies;
const Renderer = PrerenderSPAPlugin.PuppeteerRenderer;
const path = require("path");
// http://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    //seo分离模块
    //   seoPrerender({
    //   routes: [
    //     // '/',
    //     '/homePage',
    //     "/search",
    //     "/searchInfo"
    //   ], // 需要生成的路由
    //   // 预渲染输出的目录
    //   staticDir: path.join(__dirname, 'dist'),
    //   minify: true,
    //   fallback: "index.html",
    //   productionSourceMap: false,
    // }),
    compression({
      threshold: 2000, // 设置只有超过 2k 的文件才执行压缩
      deleteOriginalAssets: false, // 设置是否删除原文件
      skipIfLargerOrEqual: true, // 如果压缩后的文件大小与原文件大小一致或者更大时，不进行压缩
      // 其他的属性暂不需要配置，使用默认即可
    }),
    babel({
      babelHelpers: 'bundled',
      include: [/\.vue$/, /\.ts$/, /\.tsx$/, /.js$/],
      extensions: ['.vue', '.ts', '.js', 'tsx', 'jsx'],
      plugins: [
        '@babel/plugin-proposal-optional-chaining',
        '@babel/plugin-proposal-nullish-coalescing-operator',
        '@babel/plugin-proposal-class-properties',
        '@babel/plugin-syntax-jsx'
      ]
    }),
    UnoCSS(),
    AutoImport({
      imports: ['vue', 'vue-router'],
      //存放的位置
      dts: "src/types/auto-import.d.js",
      // resolvers: [ElementPlusResolver()],
    }),
    Components({
      // 引入组件的,包括自定义组件
      // 存放的位置
      dts: "src/types/components.d.js",
      resolvers: [ElementPlusResolver(), AntDesignVueResolver({
        importStyle: false, // css in js
      }),],
      exclude: [/node_modules/],
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    host: '0.0.0.0',
    port: 8080,
    open: true,
    proxy: {
      '/Publiccon/yzm_post_code_new': {
        target: 'http://www.gc-zb.com', //接口域名
        changeOrigin: true,       //是否跨域
      }
    }
  },
  configureWebpack: (config) => {
    if (process.env.NODE_ENV !== "production") return;
    return {
      plugins: [
        new PrerenderSPAPlugin({
          //网页包的路径将应用程序输出到prerender
          staticDir: path.join(__dirname, "dist"),
          //Routes to render 对应自己router
          routes: ["/webseo"],
          renderer: new Renderer({
            inject: {
              foo: "bar",
            },
            //渲染时显示浏览器窗口。对调试有用。
            headless: false,
            // // 在 main.js 中 document.dispatchEvent(new Event('render-event'))，两者的事件名称要对应上。
            renderAfterDocumentEvent: "render-event",
          }),
        }),
        new CompressionPlugin({
          test: /\.js$|\.html$|\.css/,
          threshold: 10240,
          deleteOriginalAssets: false,
        }),
      ],
    };
  },
});
